package com.tuyan.aixiang.component;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;

import com.tuyan.aixiang.R;
import com.tuyan.aixiang.util.ConvertUtil;

import androidx.annotation.NonNull;

public class LoadingDialog extends Dialog {

    boolean interceptBack = false;

    ImageView ivIcon;

    public LoadingDialog(@NonNull Context context) {
        super(context, R.style.OpDialog);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.loading_dialog);
        ivIcon = findViewById(R.id.iv_icon);

    }

    @Override
    public void show() {
        super.show();
        float size = ConvertUtil.dp2px(getContext(), 25) / 2;
        RotateAnimation rotateAnimation = new RotateAnimation(0f, 360f, Animation.ABSOLUTE, size, Animation.ABSOLUTE, size);
        rotateAnimation.setDuration(1000);
        rotateAnimation.setRepeatCount(Animation.INFINITE);
        rotateAnimation.setInterpolator(new LinearInterpolator());
        ivIcon.startAnimation(rotateAnimation);
    }

    public LoadingDialog setLoadingText(String text) {
        return this;
    }

    public LoadingDialog setInterceptBack(Boolean block) {
        this.interceptBack = block;
        return this;
    }

    public void close() {
        this.dismiss();
    }

    @Override
    public boolean onKeyDown(int keyCode, @NonNull KeyEvent event) {
        if(event.getAction() == KeyEvent.ACTION_DOWN) {
            if(this.interceptBack) return true;
        }
        return super.onKeyDown(keyCode, event);

    }
}
